package com.easybuyzjf.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easybuyzjf.mapper.EbProductMapper;
import com.easybuyzjf.model.EbProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class EbProductService {
    @Autowired
    private EbProductMapper ebProductMapper;

    /**
     * 分页查询商品信息
     * @param psize
     * @param limit
     * @return
     */

    public Page<EbProduct> list(int psize,int limit){
       Page<EbProduct> page=new Page(psize,limit);
//      page.addOrder(OrderItem.asc("age")); //排序
       return ebProductMapper.selectPage(page,null);
    }
    public  EbProduct detail (int epId){
        return ebProductMapper.selectById(epId);
    }

    public void addCar(int epId, HttpServletRequest request) {
            //TODO 1调用mapper获得商品对象
            EbProduct newProduct= ebProductMapper.selectById(epId);
            //TODO 2创建购物车（session）
            HttpSession session= request.getSession();

            //TODO 3得到集合？ (1.创建 2.从session获取)
            List<EbProduct> list = null;
            list=(List<EbProduct>)session.getAttribute("car");
            if(list==null){
                list= new ArrayList<>();
            }
            //TODO 4.将商品放入集合
            list.add(newProduct);
            //TODO 5.将商品放到购物车中
            session.setAttribute("car",list);
    }
}
